package cn.tedu.csmall.stock.webapi.controller;

import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.pojo.stock.dto.StockReduceCountDTO;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.commons.restful.ResponseCode;
import cn.tedu.csmall.stock.service.IStockService;
import cn.tedu.csmall.stock.webapi.until.Block;
import cn.tedu.csmall.stock.webapi.until.FallBack;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/stock")
@Api(tags = "库存管理")
public class StockController {

    @Autowired
    private IStockService stockService;

    //该请求类型是POST
    //如果想要从协议体中获取该对象，需要一个注解@RequestBody，否则获取不到对应内容(会为null)
    // http://localhost:20003/base/stock/reduce/count
    @PostMapping("/reduce/count")
    @ApiOperation("减少商品库存数")
    @SentinelResource(value = "减少商品库存数" ,
            blockHandlerClass = Block.class
            ,blockHandler ="blockFunc",
            fallbackClass = FallBack.class,
            fallback = "fallbackFunc")
    public JsonResult reduceCommodityCount(
            @RequestBody StockReduceCountDTO stockReduceCountDTO){
        // 调用业务逻辑层
        stockService.reduceCommodityCount(stockReduceCountDTO);
//        if(true){
//            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST,"请求处理失败");
//        }
        return JsonResult.ok("商品库存减少完成!");
    }

}
